#!/bin/sh
# use dmenu to choose what to unmount

# $1 = msg
dmenu_notify() {
	: | dmenu -p "$1 (press enter)"
}

dir="$(awk '$2 ~ /^\/media\// {print $2}' /etc/mtab | cut -d / -f 3 | dmenu -p "Select media device to remove")"
dir=$(printf $dir) # cleanup escaped chars
if [ -z "$dir" ] || ! [ -d "/media/$dir" ]; then
	echo "invalid selection: '$dir'"
	exit 1
fi

if result=$(umount "/media/$dir" 2>&1); then
	rmdir "/media/$dir"
	dmenu_notify "You can now safely remove $dir"
	exit 0
else
	echo "$result"
	dmenu_notify "ERROR: $result"
	exit 32
fi
